//
//  AboutViewController.m
//  Rong360
//
//  Created by tianliwei on 1/3/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "AboutViewController.h"
#import "AboutRecommendCell.h"
#import "AboutBaseCell.h"
#import "AppUnit.h"
#import "UMFeedback.h"
#import "NetCheckUtil.h"
#import "RecommendEntity.h"
@interface AboutViewController ()
@property (nonatomic, strong)NSArray *normalItems;
@property (nonatomic, strong)NSArray *otherApps;
@end

static NSString *AboutRecommendIdentifierCell = @"AboutRecommendIdentifierCell";
static NSString *AboutBaseIdentifierCell = @"AboutBaseIdentifierCell";

@implementation AboutViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"关于";
    self.tableView.backgroundView = nil;
    NSString *versionInfo = [NSString stringWithFormat:@"版本信息：%@",kAppVersion];
    self.normalItems = @[@"意见反馈",versionInfo];
    if ([NetCheckUtil isNetworkRunning] && [RecommendEntity sharedRecommend].apps.count > 2) {
        self.otherApps = @[[RecommendEntity sharedRecommend].apps[0],[RecommendEntity sharedRecommend].apps[1],[RecommendEntity sharedRecommend].apps[2]];
    }else{
        self.otherApps = [NSArray array];
    }
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AboutRecommendCell" bundle:nil] forCellReuseIdentifier:AboutRecommendIdentifierCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"AboutBaseCell" bundle:nil] forCellReuseIdentifier:AboutBaseIdentifierCell];
    self.tableView.backgroundView = nil;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 44.0f;
    }
    return 55.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 120;
    }
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return @"精彩推荐";
    }
    return nil;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *v = nil;
    if (section == 0) {
        v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 120)];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(130, 30, 60, 60)];
        imageView.image = [UIImage imageNamed:@"120"];
        [v addSubview:imageView];
    }
    return v;
}
    
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return self.normalItems.count;
        case 1:
            return self.otherApps.count;
        default:
            break;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:{
            AboutBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:AboutBaseIdentifierCell];
            cell.mainLabel.text = self.normalItems[indexPath.row];
            cell.backgroundView = [[UIView alloc]init];
            cell.backgroundColor = [UIColor whiteColor];
            if (indexPath.row == 0) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }            
            return cell;
        }
        case 1:{
            AboutRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:AboutRecommendIdentifierCell];
            cell.backgroundView = [[UIView alloc]init];
            cell.backgroundColor = [UIColor whiteColor];

            AppUnit *appUnit = [[AppUnit alloc]init];
            [appUnit parseFromDic:self.otherApps[indexPath.row]];
            cell.mainLabel.text = appUnit.name;
            cell.subLabel.text = appUnit.desc;
            [cell.thumbImg setImageWithURL:[NSURL URLWithString:appUnit.logo]];            
            return cell;
        }
        default:
            break;
    }
    return nil;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.hidesBottomBarWhenPushed = YES;
    switch (indexPath.section) {
        case 0:{
            switch (indexPath.row) {
                case 0:
                    [UMFeedback showFeedback:self withAppkey:kUMFeedbackKey];
                    break;
                default:
                    break;
            }
            break;
        }
            
        case 1:{
            
            switch (indexPath.row) {
                case 0:{
                    AppUnit *appUnit = [[AppUnit alloc]init];
                    [appUnit parseFromDic:self.otherApps[indexPath.row]];

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUnit.url]];
                    break;
                }
                case 1:{
                    AppUnit *appUnit = [[AppUnit alloc]init];
                    [appUnit parseFromDic:self.otherApps[indexPath.row]];

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUnit.url]];
                    break;
                }
                case 2:{
                    AppUnit *appUnit = [[AppUnit alloc]init];
                    [appUnit parseFromDic:self.otherApps[indexPath.row]];

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUnit.url]];
                    break;
                }
                default:
                    break;
            }
            
        }
        default:
            break;
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
